package com.yiren.dbaa.view.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.dbaa.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class SingleLightAdapter extends BaseAdapter implements  CompoundButton.OnCheckedChangeListener{

    private Context context;//上下文
    private List<SingleLightEntity> singLightList;//单项灯光list
    private int AdapterType;//1:单项灯光 2:灯光组合 3:选择弹出框 4:弹出框单选
    public static List<Integer> sSelectPositionList;//灯光列表选择的序号list
    private Activity activity;//activity
    private List<CheckBox> mCheckBoxList;//存放listview里checkbox的list

    private List<String> checkedList = new ArrayList<>();//存放已选中的checkbox

    public SingleLightAdapter(Context context, List<SingleLightEntity> list, int type) {
        this.context = context;
        this.singLightList = list;
        this.AdapterType = type;
        sSelectPositionList = new ArrayList<Integer>();
        mCheckBoxList = new ArrayList<CheckBox>();
    }

    public SingleLightAdapter(Activity activity, Context context, List<SingleLightEntity> list, int type) {
        this.activity = activity;
        this.context = context;
        this.singLightList = list;
        this.AdapterType = type;
        sSelectPositionList = new ArrayList<Integer>();
        mCheckBoxList = new ArrayList<CheckBox>();
    }

    @Override
    public int getCount() {
        return singLightList.size();
    }

    @Override
    public Object getItem(int position) {
        return singLightList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void remove(int arg0) {// 删除指定位置的item
        singLightList.remove(arg0);
        this.notifyDataSetChanged();// 不要忘记更改适配器对象的数据源
    }

    public void insert(SingleLightEntity item, int arg0) {// 在指定位置插入item
        singLightList.add(arg0, item);
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder viewHolder;
            if (null == convertView) {
                convertView = View.inflate(context, R.layout.list_singlelight, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_sl_id = (TextView) convertView.findViewById(R.id.tv_sl_id);
                viewHolder.tv_sl_voice = (TextView) convertView.findViewById(R.id.tv_sl_voice);
                viewHolder.tv_sl_operation = (TextView) convertView.findViewById(R.id.tv_sl_operation);
                viewHolder.tv_sl_ctime = (TextView) convertView.findViewById(R.id.tv_sl_ctime);
                viewHolder.tv_sl_points = (TextView) convertView.findViewById(R.id.tv_sl_points);
                viewHolder.tv_sl_grade = (TextView) convertView.findViewById(R.id.tv_sl_grade);
                viewHolder.tv_sl_modify = (TextView) convertView.findViewById(R.id.tv_sl_modify);
                viewHolder.tv_sl_delete = (TextView) convertView.findViewById(R.id.tv_sl_delete);
                viewHolder.cb_grouplight_select = (CheckBox) convertView.findViewById(R.id.cb_grouplight_select);
                viewHolder.ll_sl_md = (LinearLayout) convertView.findViewById(R.id.li_sl_md);
                viewHolder.iv_grouplight_dragsort = (ImageView) convertView.findViewById(R.id.iv_grouplight_dragsort);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (AdapterType == 2) {
                viewHolder.tv_sl_id.setBackgroundColor(0);
                viewHolder.tv_sl_modify.setText("替换");
                viewHolder.iv_grouplight_dragsort.setVisibility(View.VISIBLE);
            } else if (AdapterType == 3 || AdapterType == 4) {
                viewHolder.tv_sl_id.setVisibility(View.GONE);
                viewHolder.ll_sl_md.setVisibility(View.GONE);
                viewHolder.cb_grouplight_select.setVisibility(View.VISIBLE);
            }
            viewHolder.tv_sl_id.setText(String.valueOf(position + 1));
            viewHolder.tv_sl_voice.setText(singLightList.get(position).getContent());
            viewHolder.tv_sl_operation.setText("操作: " + singLightList.get(position).getStateValueShow());
            viewHolder.tv_sl_ctime.setText("完成时间: " + singLightList.get(position).getWaitTime() + "S");
            if (!(singLightList.get(position).getGradeStandardEntity() == null)) {
                viewHolder.tv_sl_points.setText("扣分: " + singLightList.get(position).getGradeStandardEntity().getStandardContent());
                viewHolder.tv_sl_grade.setText("-" + singLightList.get(position).getGradeStandardEntity().getGrade() + "分");
            } else {
                viewHolder.tv_sl_points.setText("扣分: " + "无");
                viewHolder.tv_sl_grade.setText("");
            }
        if (mCheckBoxList.size()< singLightList.size()) {
            mCheckBoxList.add(viewHolder.cb_grouplight_select);
        }
        mCheckBoxList = removeDuplicate(mCheckBoxList);
            //单选
            if (AdapterType == 4) {
                viewHolder.cb_grouplight_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < mCheckBoxList.size(); i++) {
                            if (position == i) {
                                mCheckBoxList.get(i).setChecked(true);
                            } else {
                                mCheckBoxList.get(i).setChecked(false);
                            }
                        }
                        sSelectPositionList.clear();
                        sSelectPositionList.add(position);
                    }
                });

            }

            viewHolder.tv_sl_modify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemModifyClickListener != null) {
                        mOnItemModifyClickListener.onItemModifyClick(view, position);
                    }
                }
            });

            viewHolder.tv_sl_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemDeleteClickListener != null) {
                        mOnItemDeleteClickListener.onItemDeleteClick(v, position);
                    }
                }
            });

            viewHolder.cb_grouplight_select.setTag(viewHolder.cb_grouplight_select.getId(),position);
            viewHolder.cb_grouplight_select.setOnCheckedChangeListener(this);
            viewHolder.cb_grouplight_select.setChecked(checkedList.contains(position+""));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int position = (int)buttonView.getTag(buttonView.getId());
        if (isChecked&&!checkedList.contains(position+"")){
            checkedList.add(position+"");
            sSelectPositionList.add(position);
            sSelectPositionList = removeDuplicate_int(sSelectPositionList);
        }else {
            checkedList.remove(position+"");
            for (int i = 0; i < sSelectPositionList.size(); i++) {
                if (sSelectPositionList.get(i) == position) {
                    sSelectPositionList.remove(i);
                }
            }
        }
    }


    public static class ViewHolder {
        TextView tv_sl_id;//单项灯光ID
        TextView tv_sl_voice;//播报语音
        TextView tv_sl_operation;//灯光操作
        TextView tv_sl_ctime;//完成时间
        TextView tv_sl_points;//扣分
        TextView tv_sl_grade;
        TextView tv_sl_modify;//修改
        TextView tv_sl_delete;//删除
        LinearLayout ll_sl_md;
        CheckBox cb_grouplight_select;//选择框
        ImageView iv_grouplight_dragsort;//拖拽排序

    }

    //修改点击事件回调
    public interface OnItemModifyClickListener {
        void onItemModifyClick(View view, int position);
    }

    private OnItemModifyClickListener mOnItemModifyClickListener;

    public void setOnItemModifyClickListener(OnItemModifyClickListener onItemModifyClickListener) {
        mOnItemModifyClickListener = onItemModifyClickListener;
    }

    //删除点击事件回调
    public interface OnItemDeleteClickListener {
        void onItemDeleteClick(View view, int position);
    }

    private OnItemDeleteClickListener mOnItemDeleteClickListener;

    public void setOnItemDeleteClickListener(OnItemDeleteClickListener onItemDeleteClickListener) {
        mOnItemDeleteClickListener = onItemDeleteClickListener;
    }


    //条目点击事件回调
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }


    /**
     * 刷新Adapter数据
     *
     * @param lights
     */
    public void reflesh(List<SingleLightEntity> lights) {
        this.singLightList = lights;
        this.notifyDataSetChanged();
    }

    /**
     * list去重
     *
     * @param list
     * @return
     */
    public static List<CheckBox> removeDuplicate(List<CheckBox> list) {
        Set set = new LinkedHashSet<CheckBox>();
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    public static List<Integer> removeDuplicate_int(List<Integer> list) {
        Set set = new LinkedHashSet<Integer>();
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }


}
